package cn.sirun.com.fca.personal.center.presenter.impl;


import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.util.rx.SimpleObserver;

import cn.sirun.com.fca.account.constant.AccountActionConst;
import cn.sirun.com.fca.account.model.base.SubmitPhoneRequest;
import cn.sirun.com.fca.account.model.base.SubmitPhoneResponse;
import cn.sirun.com.fca.account.model.login.LoginResponse;
import cn.sirun.com.fca.account.model.register.RegisterSubmitResponse;
import cn.sirun.com.fca.account.model.register.SubmitEmailRequest;
import cn.sirun.com.fca.account.observer.CommonExceptionObserver;
import cn.sirun.com.fca.account.presenter.impl.PhonePresenter;
import cn.sirun.com.fca.personal.center.constant.PersonalCenterActionConst;
import cn.sirun.com.fca.personal.center.loader.ChangeRegisterAccontLoader;
import cn.sirun.com.fca.personal.center.model.request.ChangeAccountResendRequest;
import cn.sirun.com.fca.personal.center.model.request.IdentityNumberRequest;
import cn.sirun.com.fca.personal.center.model.request.SubmitPhonePinRequest;
import cn.sirun.com.fca.personal.center.model.response.SubmitCodeResponse;
import cn.sirun.com.fca.personal.center.presenter.IChangeRegisterAccountPresenter;
import cn.sirun.com.fca.personal.center.presenter.PersonalCenterFlowPresenter;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.subjects.BehaviorSubject;

public class ChangeRegisterAccountPresenter extends PersonalCenterFlowPresenter<ChangeRegisterAccontLoader>
        implements IChangeRegisterAccountPresenter {
    private BehaviorSubject<BaseResponse> mBehaviorSubject = BehaviorSubject.create();

    @Override
    protected ChangeRegisterAccontLoader createLoader() {
        return new ChangeRegisterAccontLoader();
    }

    @Override
    public void submitIdentityNumber(String identityNumber) {
        getLoader().submitIdentityNumber(new IdentityNumberRequest(identityNumber))
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<Void>>() {

                    @Override
                    public void onNext(BaseResponse<Void> submitIdentityNumResponse) {
                        super.onNext(submitIdentityNumResponse);
                        getActionListener().onAction
                                (PersonalCenterActionConst.ACTION_SUBMIT_IDENTITYNUMBER_RESPONSE,
                                        submitIdentityNumResponse);
                    }
                });
    }

    @Override
    public void changeAccountPhone(SubmitPhoneRequest request) {
        getLoader().changeAccountPhone(request)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<SubmitPhoneResponse>>() {

                    @Override
                    public void onNext(BaseResponse<SubmitPhoneResponse> submitPhoneResponse) {
                        super.onNext(submitPhoneResponse);
                        getActionListener().onAction
                                (PersonalCenterActionConst.ACTION_CHANGE_ACCOUNT_PHONE_RESPONSE,
                                        submitPhoneResponse);
                    }
                });
    }

    @Override
    public void changeAccountEmail(SubmitEmailRequest request) {
        getLoader().changeAccountEmail(request)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<SubmitPhoneResponse>>() {

                    @Override
                    public void onNext(BaseResponse<SubmitPhoneResponse> submitPhoneResponse) {
                        super.onNext(submitPhoneResponse);
                        getActionListener().onAction
                                (PersonalCenterActionConst.ACTION_CHANGE_ACCOUNT_PHONE_RESPONSE,
                                        submitPhoneResponse);
                    }
                });
    }

    @Override
    public void changeAccountResend(ChangeAccountResendRequest request) {
        getLoader().changeAccountResend(request)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<SubmitPhoneResponse>>() {

                    @Override
                    public void onNext(BaseResponse<SubmitPhoneResponse> submitPhoneResponse) {
                        super.onNext(submitPhoneResponse);
                        getActionListener().onAction
                                (PersonalCenterActionConst.ACTION_CHANGE_ACCOUNT_PHONE_RESPONSE,
                                        submitPhoneResponse);
                    }
                });
    }

    @Override
    public void changeAccountSubmitCaptcha(String code) {
        getLoader().changeAccountSubmitCaptcha(code)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<RegisterSubmitResponse>>() {

                    @Override
                    public void onNext(BaseResponse<RegisterSubmitResponse> submitCaptchaResponse) {
                        super.onNext(submitCaptchaResponse);
                        getActionListener().onAction
                                (PersonalCenterActionConst.ACTION_CHANGE_ACCOUNT_SUBMIT_CAPTCHA_RESPONSE,
                                        submitCaptchaResponse);
                    }
                });
    }


    @Override
    public void submitBindAccountCaptcha(String code) {
        getLoader().submitBindAccountCaptcha(code)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<RegisterSubmitResponse>>() {

                    @Override
                    public void onNext(BaseResponse<RegisterSubmitResponse> submitCaptchaResponse) {
                        super.onNext(submitCaptchaResponse);
                        getActionListener().onAction
                                (PersonalCenterActionConst.ACTION_CHANGE_ACCOUNT_SUBMIT_CAPTCHA_RESPONSE,
                                        submitCaptchaResponse);
                    }
                });
    }

    @Override
    public void bindAccountResend() {
        getLoader().bindAccountResend()
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<SubmitPhoneResponse>>() {

                    @Override
                    public void onNext(BaseResponse<SubmitPhoneResponse> submitPhoneResponse) {
                        super.onNext(submitPhoneResponse);
                        getActionListener().onAction
                                (PersonalCenterActionConst.ACTION_CHANGE_ACCOUNT_RESEND_RESPONSE,
                                        submitPhoneResponse);
                    }
                });
    }

    @Override
    public void submitPhonePin(SubmitPhoneRequest request) {
        getLoader().submitPhonePinCaptcha(request)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<SubmitPhoneResponse>>() {

                    @Override
                    public void onNext(BaseResponse<SubmitPhoneResponse> submitPhoneResponse) {
                        super.onNext(submitPhoneResponse);
                        getActionListener().onAction
                                (PersonalCenterActionConst.ACTION_SUBMIT_PHONE_PIN,
                                        submitPhoneResponse);
                    }
                });
    }

    @Override
    public void submitPinVerifyCaptcha(String code) {
        getLoader().submitVerifyCaptcha(code)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<SubmitCodeResponse>>() {

                    @Override
                    public void onNext(BaseResponse<SubmitCodeResponse> submitCaptchaResponse) {
                        super.onNext(submitCaptchaResponse);
                        getActionListener().onAction
                                (PersonalCenterActionConst.ACTION_SUBMIT_VERIFY_CODE_PIN,
                                        submitCaptchaResponse);
                    }
                });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mBehaviorSubject.onComplete();
    }
}
